package com.fling.abcde.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.view.ViewGroup;

import com.fling.abcde.R;
import com.fling.abcde.base.BaseFragment;
import com.fling.abcde.databinding.FragmentHome2Binding;
import com.lib.common.adapter.BaseRecyclerAdapter;
import com.lib.utils.XLog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author FengLing
 * Created on 2017-08-24 17:23
 */
public class Home2Fragment extends BaseFragment<FragmentHome2Binding> implements View.OnClickListener, OnRefreshListener {
    private RecyclerView mBehaviorDemoRecycler;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Context context = getContext();

        mBehaviorDemoRecycler = thisViewBinding.ccHome2.behaviorDemoRecycler;
//        mBehaviorDemoSwipeRefresh = thisViewBinding.ccHome2.behaviorDemoSwipeRefresh;
//        mFab = thisViewBinding.ccHome2.fab;

//        mBehaviorDemoSwipeRefresh.setOnRefreshListener(this);
//        mFab.setOnClickListener(this);

        mBehaviorDemoRecycler.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        InnerAdapter adapter = new InnerAdapter(context);
        List<TestData> list = new ArrayList<>();
        list.add(new TestData("0", 0));
        list.add(new TestData("0", 0));
        list.add(new TestData("0", 1));
        list.add(new TestData("0", 1));
        list.add(new TestData("0", 2));
        list.add(new TestData("0", 2));
        list.add(new TestData("0", 0));
        list.add(new TestData("0", 0));
        list.add(new TestData("0", 0));
        list.add(new TestData("0", 0));
        list.add(new TestData("0", 0));
        list.add(new TestData("0", 0));
        adapter.setData(list);
        adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XLog.i("v = " + v);
            }
        });
        mBehaviorDemoRecycler.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.fab:
//
//                break;
        }
    }


    @Override
    public void onRefresh(RefreshLayout rl) {
        rl.finishRefresh();
    }

    private static class InnerAdapter extends BaseRecyclerAdapter<TestData> {
        public InnerAdapter(Context context) {
            super(context);
        }

        @Override
        protected BaseViewHolder initView(ViewGroup rootView, int viewType) {
            switch (viewType) {
                default:
                case 0: {
                    View view = getViewByLayoutResId(rootView, R.layout.item_1);
                    return new ViewHolder(view);
                }
                case 1: {
                    View view = getViewByLayoutResId(rootView, R.layout.item_img);
                    return new ImageViewHolder(view);
                }
                case 2: {
                    View view = getViewByLayoutResId(rootView, R.layout.item_2);
                    return new ViewHolder2(view);
                }
            }
        }

        @Override
        public boolean onBindViewHolder(BaseViewHolder<TestData> holder, TestData data, int position) {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).mText.setText("第" + position + "条数据");
            } else if (holder instanceof ViewHolder2) {
                ((ViewHolder2) holder).mText.setText("第" + position + "条数据");
            } else if (holder instanceof ImageViewHolder) {
                ((ImageViewHolder) holder).mText.setText("第" + position + "条数据");
            } else {
            }
            return true;
        }
    }
}